package com.oa.config;

import com.google.gson.Gson;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: 一拳超人
 * @Date: 2021/10/14 20:15
 */
@Configuration
@EnableConfigurationProperties(QiniuConfigProperties.class)
public class QiniuUploadFileConfig {
    @Autowired
    private QiniuConfigProperties qiniuConfigProperties;

    /**
     * 配置空间所在区域
     *
     * @return 配置
     */
    @Bean
    public com.qiniu.storage.Configuration qiniuConfig() {
        return new com.qiniu.storage.Configuration(Region.regionAs0());
    }

    /**
     * 构建一个七牛上传工具实例
     */
    @Bean
    public UploadManager uploadManager() {
        return new UploadManager(qiniuConfig());
    }

    /**
     * 配置认证信息,AccessKey和SecretKey
     *
     * @return 认证信息
     */
    @Bean
    public Auth auth() {
        return Auth.create(qiniuConfigProperties.getAccessKey(), qiniuConfigProperties.getSecretKey());
    }

    /**
     * 构建七牛空间管理实例
     */
    @Bean
    public BucketManager bucketManager() {
        return new BucketManager(auth(), qiniuConfig());
    }

    @Bean
    public Gson gson() {
        return new Gson();
    }
}
